/******************************************************************************* 
 *  Copyright 2008 Amazon Technologies, Inc.
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  
 *  You may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 *  specific language governing permissions and limitations under the License.
 * ***************************************************************************** 
 *    __  _    _  ___ 
 *   (  )( \/\/ )/ __)
 *   /__\ \    / \__ \
 *  (_)(_) \/\/  (___/
 * 
 *  Amazon Simple DB CSharp Library
 *  API Version: 2007-11-07
 *  Generated: Thu Mar 19 02:28:00 PDT 2009 
 * 
 */

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
using Attribute = Amazon.SimpleDB.Model.Attribute;


namespace Amazon.SimpleDB.Samples
{

    /// <summary>
    /// Query With Attributes  Samples
    /// </summary>
    public class QueryWithAttributesSample
    {
    
                                                                 
        /// <summary>
        /// The QueryWithAttributes operation returns a set of item names and associated attributes.
        /// The query semantics of this operation are identical to the Query operation.
        /// 
        /// </summary>
        /// <param name="service">Instance of AmazonSimpleDB service</param>
        /// <param name="request">QueryWithAttributesRequest request</param>
        public static void InvokeQueryWithAttributes(AmazonSimpleDB service, QueryWithAttributesRequest request)
        {
            try 
            {
                QueryWithAttributesResponse response = service.QueryWithAttributes(request);
                
                
                Console.WriteLine ("Service Response");
                Console.WriteLine ("=============================================================================");
                Console.WriteLine ();

                Console.WriteLine("        QueryWithAttributesResponse");
                if (response.IsSetQueryWithAttributesResult()) 
                {
                    Console.WriteLine("            QueryWithAttributesResult");
                    QueryWithAttributesResult  queryWithAttributesResult = response.QueryWithAttributesResult;
                    List<Item> itemList = queryWithAttributesResult.Item;
                    foreach (Item item in itemList) 
                    {
                        Console.WriteLine("                Item");
                        if (item.IsSetName()) 
                        {
                            Console.WriteLine("                    Name");
                            Console.WriteLine("                        {0}", item.Name);
                        }
                        List<Attribute> attributeList = item.Attribute;
                        foreach (Attribute attribute in attributeList) 
                        {
                            Console.WriteLine("                    Attribute");
                            if (attribute.IsSetName()) 
                            {
                                Console.WriteLine("                        Name");
                                Console.WriteLine("                            {0}", attribute.Name);
                            }
                            if (attribute.IsSetValue()) 
                            {
                                Console.WriteLine("                        Value");
                                Console.WriteLine("                            {0}", attribute.Value);
                            }
                        }
                    }
                    if (queryWithAttributesResult.IsSetNextToken()) 
                    {
                        Console.WriteLine("                NextToken");
                        Console.WriteLine("                    {0}", queryWithAttributesResult.NextToken);
                    }
                } 
                if (response.IsSetResponseMetadata()) 
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata  responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId()) 
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                    if (responseMetadata.IsSetBoxUsage()) 
                    {
                        Console.WriteLine("                BoxUsage");
                        Console.WriteLine("                    {0}", responseMetadata.BoxUsage);
                    }
                } 

            } 
            catch (AmazonSimpleDBException ex) 
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Box Usage: " + ex.BoxUsage);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
            }
        }
        }
}
